package com.drcoffee.drcoffeemall.util;

import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

public class WeClientRequestEnUtil {

    private static final String init_url = "http://172.66.2.80:8082/longRange";
    private static final WebClient webClient = WebClient.create();


    public static String initiateGET(String path, Object requestBody) {

        // 指定URI和查询参数
        String url = init_url + path;

        // 使用 UriComponentsBuilder 来构建带查询参数的 URI

        // 发起POST请求
        String responseBody = webClient.post()
                .uri(url)
                .contentType(MediaType.APPLICATION_JSON) // 设置请求体类型
                .body(BodyInserters.fromValue(requestBody)) // 设置请求体内容
                .retrieve()
                .bodyToMono(String.class)
                .block(); // 阻塞等待响应

        // 处理响应
        System.out.println("Response: " + responseBody);

        return responseBody;
    }

    public static String initiatePOST(String path) {

        // 指定URI和查询参数
        String url = init_url + path;

        // 发起GET请求
        String responseBody = webClient.get()
                .uri(url)
                .retrieve()
                .bodyToMono(String.class)
                .block(); // 阻塞等待响应

        // 处理响应
        System.out.println("Response: " + responseBody);
        return responseBody;
    }
}
